//
//  CYSpecialCollectionCell.m
//  KCZ_B段项目
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 Sky. All rights reserved.
//

#import "CYSpecialCollectionCell.h"
#import "FitHeader.h"
#import "CYSpecialTableViewCell.h"
#import "HTTPMethod.h"
#import "changeString.h"
#import "CYSpecialModel.h"
#import <MJRefresh.h>
@implementation CYSpecialCollectionCell

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _dataArray = [NSMutableArray array];
        [self creatViews];
        [self getData];
        _page += 0;
        _tableView.footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            _page += 20;
            [self getData];
        }];
    }
    return self;
}

-(void)getData
{
    NSString *PostString = @"http://api.haodou.com/index.php?appid=2&appkey=9ef269eec4f7a9d07c73952d06b5413f&format=json&sessionid=1446294132243&vc=77&vn=5.4.0&loguid=0&deviceid=haodou359786058377939&uuid=087c7496ab334bd9babe6eb99709c4f8&channel=baidu_v540&method=Recipe.getAlbumList&virtual=&signmethod=md5&v=2&timestamp=1446296670&nonce=0.4978184168439842&appsign=152e2cc3dcd80789dce9e6e69cfa81ad";
    NSString *bodyString = [NSString stringWithFormat:@"limit=20&offset=%ld&uuid=087c7496ab334bd9babe6eb99709c4f8", _page];
    [HTTPMethod getDataByString:PostString BodyDic:[changeString changeStringToDictionary:bodyString] WithDataBlock:^(id data) {
        
        NSDictionary *dic = [data objectForKey:@"result"];
        NSArray *array = [dic objectForKey:@"list"];
        if (array.count != 0) {
            for (NSDictionary *dataDic in array) {
                CYSpecialModel *Special = [[CYSpecialModel alloc]init];
                [Special setValuesForKeysWithDictionary:dataDic];
                [_dataArray addObject:Special];
            }
        }
        [_tableView.footer endRefreshing];
        [_tableView reloadData];
    }];
}

-(void)creatViews
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height - 49 * ht) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [self addSubview:_tableView];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 120 * ht;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    CYSpecialTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (!cell) {
        cell = [[CYSpecialTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    if (_dataArray.count != 0) {
        cell.Special = _dataArray[indexPath.row];
    }
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (_dataArray.count != 0) {
        NSString *string = [[_dataArray[indexPath.row]Id] stringValue];
        [_delegate passIdString:string];
    }
}


@end
